﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace asMeishi.web.Models
{
    public class tagLibrary : MongoDocument
    {
        public string tag { get; set; }
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime datetime { get; set; }
        public ObjectId userID { get; set; }
        public int num { get; set; }
    }

    public class tagLibraryC 
    {
        DataSession<MongoDataContext, tagLibrary> db = new DataSession<MongoDataContext, tagLibrary>();



        public ObjectId add(string tag)
        {
            tag = tag.Trim(' ', '　', '\n').Replace(',', '，');
            tagLibrary tagLibrary;

            tagLibrary = db.Get(d => d.tag == tag);
            if (tagLibrary != null)
            {
                tagLibrary.num++;
                db.Save(tagLibrary);
                return tagLibrary.Id;
            }

            tagLibrary = new tagLibrary();
            tagLibrary.tag = tag;
            tagLibrary.num = 1;
            tagLibrary.datetime = DateTime.Now;
            tagLibrary.userID = userC.loginUser.Id;
            db.Save(tagLibrary);
            return tagLibrary.Id;
        }


    }

}